#!/bin/bash

# Dify知识库管理系统 - 集成测试脚本
# 用于测试所有功能模块的完整流程

echo "=========================================="
echo "Dify知识库管理系统 - 集成测试"
echo "=========================================="
echo ""

# 颜色定义
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 后端服务地址
BASE_URL="http://localhost:8080"

# 测试用户ID
USER_ID=1

# 测试计数器
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0

# 测试函数
test_api() {
    local test_name=$1
    local method=$2
    local endpoint=$3
    local data=$4
    
    TOTAL_TESTS=$((TOTAL_TESTS + 1))
    echo -e "${YELLOW}测试 $TOTAL_TESTS: $test_name${NC}"
    
    if [ "$method" == "GET" ]; then
        response=$(curl -s -w "\n%{http_code}" -X GET "$BASE_URL$endpoint")
    elif [ "$method" == "POST" ]; then
        response=$(curl -s -w "\n%{http_code}" -X POST "$BASE_URL$endpoint" \
            -H "Content-Type: application/json" \
            -d "$data")
    elif [ "$method" == "DELETE" ]; then
        response=$(curl -s -w "\n%{http_code}" -X DELETE "$BASE_URL$endpoint")
    fi
    
    http_code=$(echo "$response" | tail -n1)
    body=$(echo "$response" | sed '$d')
    
    if [ "$http_code" -ge 200 ] && [ "$http_code" -lt 300 ]; then
        echo -e "${GREEN}✓ 通过 (HTTP $http_code)${NC}"
        echo "响应: $body"
        PASSED_TESTS=$((PASSED_TESTS + 1))
    else
        echo -e "${RED}✗ 失败 (HTTP $http_code)${NC}"
        echo "响应: $body"
        FAILED_TESTS=$((FAILED_TESTS + 1))
    fi
    echo ""
}

# 检查后端服务是否运行
echo "1. 检查后端服务状态..."
if curl -s "$BASE_URL/actuator/health" > /dev/null 2>&1 || curl -s "$BASE_URL" > /dev/null 2>&1; then
    echo -e "${GREEN}✓ 后端服务正在运行${NC}"
else
    echo -e "${RED}✗ 后端服务未运行，请先启动后端服务${NC}"
    echo "启动命令: mvn spring-boot:run"
    exit 1
fi
echo ""

# 测试问答功能
echo "=========================================="
echo "2. 测试问答功能"
echo "=========================================="
test_api "提交问题" "POST" "/api/qa/ask" '{"question":"什么是Spring Boot?","userId":1}'
test_api "获取问答历史" "GET" "/api/qa/history?page=0&size=10"
test_api "按用户筛选历史" "GET" "/api/qa/history?page=0&size=10&userId=1"

# 测试未解答问题管理
echo "=========================================="
echo "3. 测试未解答问题管理"
echo "=========================================="
test_api "获取未解答问题列表" "GET" "/api/unanswered/list?page=0&size=10"

# 注意：以下测试需要先有未解答问题
# test_api "提交人工解答" "POST" "/api/unanswered/1/answer" '{"answer":"这是人工解答","userId":1}'
# test_api "获取问题的所有解答" "GET" "/api/unanswered/1/answers"
# test_api "对解答评分" "POST" "/api/unanswered/answer/1/rate" '{"rating":"useful","userId":2}'

# 测试SOP生成功能
echo "=========================================="
echo "4. 测试SOP生成功能"
echo "=========================================="
test_api "生成Markdown格式SOP" "POST" "/api/sop/generate" '{"topic":"如何使用Spring Boot","format":"markdown","userId":1}'

# 测试文档管理功能
echo "=========================================="
echo "5. 测试文档管理功能"
echo "=========================================="
test_api "获取文档列表" "GET" "/api/documents/list?page=0&size=10"

# 注意：文件上传测试需要实际文件
# echo "测试文档上传..."
# curl -X POST "$BASE_URL/api/documents/upload" \
#     -F "file=@test.pdf" \
#     -F "userId=1"

# 测试结果汇总
echo "=========================================="
echo "测试结果汇总"
echo "=========================================="
echo "总测试数: $TOTAL_TESTS"
echo -e "${GREEN}通过: $PASSED_TESTS${NC}"
echo -e "${RED}失败: $FAILED_TESTS${NC}"
echo ""

if [ $FAILED_TESTS -eq 0 ]; then
    echo -e "${GREEN}所有测试通过！${NC}"
    exit 0
else
    echo -e "${RED}部分测试失败，请检查日志${NC}"
    exit 1
fi
